package com.xnfh.common.adapter

import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.xnfh.common.R
import com.xnfh.common.databinding.ItemSelectImageBinding
import com.xnfh.image.ImageHelper
import java.text.FieldPosition

/**
 * @Description:选择图片的adapter展示
 * @Author: ziheng
 * @CreateDate: 2023/12/19 09:33
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/19 09:33
 * @UpdateRemark: 更新说明
 */
class SelectImageAdapter(val maxSize: Int): BaseQuickAdapter<String,BaseDataBindingHolder<ItemSelectImageBinding>>(R.layout.item_select_image) {

    companion object{
        //这个代表添加图片的view条目数据
        private val NULL_IMAGE = "NULL"
    }
    private var callBack: SelectImageCallBack? = null

    override fun convert(holder: BaseDataBindingHolder<ItemSelectImageBinding>, item: String) {
        val dataBinding = holder.dataBinding
        holder.setGone(R.id.item_select_delete,item == NULL_IMAGE)
        if(item == NULL_IMAGE){
            ImageHelper.instance?.loadResourceImage(context,R.mipmap.add_image,dataBinding?.itemSelectImageView)
        }else{
            ImageHelper.instance?.loadNetImg(context,item,dataBinding?.itemSelectImageView,R.mipmap.image_error,R.mipmap.image_error)
        }
        dataBinding?.itemSelectImageView?.setOnClickListener {
            if(item == NULL_IMAGE){
                callBack?.addImageClick()
            }else{
                callBack?.showBigImage(holder.layoutPosition,dataBinding.itemSelectImageView)
            }
        }
        dataBinding?.itemSelectDelete?.setOnClickListener {
            callBack?.deleteImagePosition(holder.layoutPosition)
            data.removeAt(holder.layoutPosition)
            if(data[data.lastIndex] != NULL_IMAGE && data.size < maxSize){
                data.add(NULL_IMAGE)
            }
            notifyDataSetChanged()
        }
    }

    override fun setNewInstance(list: MutableList<String>?) {
        if(list == null){
            //如果传入的数据为null，就创建一个集合，并且把添加图片的加号添加进去
            val newList = ArrayList<String>()
            newList.add(0,NULL_IMAGE)
            super.setNewInstance(newList)
        }else{
            //如果传入的list不是空，就判断一下改不改展示加号
            if(list.size < maxSize){
                list.add(NULL_IMAGE)
            }
            super.setNewInstance(list)
        }
    }

    /**
     * 重写此方法
     */
    override fun addData(data: String) {
        this.data.add(data)
        //如果添加的图片大于最大数，就把最后一条数据删除（添加图片的加号）
        if(this.data.size > maxSize){
            this.data.removeLast()
        }
        notifyItemInserted(this.data.size + headerLayoutCount)
        compatibilityDataSizeChanged(1)
    }

    interface SelectImageCallBack{
        fun addImageClick()
        fun showBigImage(position: Int,imageView: ImageView)
        fun deleteImagePosition(position: Int)
    }
    fun setCallBack(listener: SelectImageCallBack){
        callBack = listener
    }

}